<template>
  <div>
    <div class="pageTopTitle">活动管理</div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="活动主题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入活动主题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="app-container mt20">

      <el-row :gutter="10" class="mb20">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="activityList">
        <el-table-column label="活动主题" prop="title"/>
        <el-table-column label="活动时间" >
          <template slot-scope="scope">
            <div>{{ scope.row.startAt }} </div>
            <div> - </div>
            <div>{{ scope.row.endAt }} </div>
          </template>
        </el-table-column>
        <el-table-column label="活动地点" prop="address"/>
        <el-table-column label="活动专家"  >
          <template slot-scope="scope">
              <div v-if="scope.row.expertList && scope.row.expertList.length === 0">
                <el-link  type="success" style="font-size: 18px;" :underline="true">0</el-link>
              </div>
              <div v-else style="display: flex; flex-wrap: wrap;">
                <el-tag v-for="expert in scope.row.expertList" style="margin-right: 5px; margin-bottom: 5px" :key="expert.id" type="warning">{{ expert.name }}</el-tag>
              </div>
            <el-button
              size="mini"
              type="text"
              @click="openExpert(scope.row)"
            >添加
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="活动客户"  >
          <template slot-scope="scope">
            <div v-if="scope.row.clientList && scope.row.clientList.length === 0">
              <el-link  type="success" style="font-size: 18px;" :underline="true">0</el-link>
            </div>
            <div v-else style="display: flex; flex-wrap: wrap;">
              <el-tag v-for="expert in scope.row.clientList" style="margin-right: 5px; margin-bottom: 5px" :key="expert.id" type="warning">{{ expert.name }}</el-tag>
            </div>
            <el-button
              size="mini"
              type="text"
              @click="openClient(scope.row)"
            >添加
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="活动介绍"  prop="desc"/>
        <el-table-column label="操作" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改活动对话框 -->
      <el-drawer
        :title="title"
        :visible.sync="open" :wrapper-closable="false"
        size="35%"
      >
        <div class="drawer-content">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="活动主题" prop="title">
              <el-input v-model="form.title" placeholder="请输入活动主题"/>
            </el-form-item>
            <el-form-item label="开始时间" prop="startAt">
              <el-date-picker
                style="width: 100%"
                format="yyyy-MM-dd HH:mm"
                v-model="form.startAt"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm"
                placeholder="请输入开始时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endAt">
              <el-date-picker
                style="width: 100%"
                format="yyyy-MM-dd HH:mm"
                v-model="form.endAt"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm"
                placeholder="请输入结束时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="活动地点" prop="address">
              <el-input v-model="form.address" placeholder="请输入活动地点"/>
            </el-form-item>
            <el-form-item label="活动介绍" prop="desc">
              <el-input v-model="form.desc" type="textarea" rows="3" placeholder="请输入活动介绍"/>
            </el-form-item>
            <el-form-item label="活动相册" prop="images">
              <ImageUpload v-model="form.images" :limit="10"/>
            </el-form-item>
          </el-form>
        </div>
        <div class="drawer-footer">
          <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
          <el-button size="small" @click="cancel">取 消</el-button>
        </div>
      </el-drawer>

      <el-dialog title="选择专家" :visible.sync="expertDialogVisible" center width="800px">
        <div class="text-center mb20">
          <el-transfer
            style="text-align: left; display: inline-block"
            v-model="editExpertForm.expertIdList"
            filterable
            :titles="['专家', '已选择']"
            :button-texts="['到左边', '到右边']"
            :data="expertList">
          </el-transfer>
        </div>
        <div style="text-align: center">
          <el-button type="primary" @click="subExpertCheck">确认选择</el-button>
        </div>
      </el-dialog>


      <el-dialog title="选择客户" :visible.sync="clientDialogVisible" center width="800px">
        <div class="text-center mb20">
          <el-transfer
            style="text-align: left; display: inline-block"
            v-model="editClientForm.clientIdList"
            filterable
            :titles="['客户', '已选择']"
            :button-texts="['到左边', '到右边']"
            :data="clientList">
          </el-transfer>
        </div>
        <div style="text-align: center">
          <el-button type="primary" @click="subClientCheck">确认选择</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity ,editExpert,editClient} from '@/api/web/activity'

import { listClient } from '@/api/web/client'
import { listExpert } from '@/api/web/expert'
export default {
  name: 'Activity',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      activityList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      expertList:[],
      clientList: [],

      expertDialogVisible: false,
      clientDialogVisible: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        startAt: null,
        endAt: null,
        address: null,
        channelId: null,
        images: null,
        desc: null,
        createAt: null,
        createUser: null
      },
      // 表单参数
      form: {},
      editExpertForm:{
        activityId: null,
        expertIdList: []
      },
      editClientForm:{
        activityId: null,
        clientIdList: []
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: '请输入活动主题', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入活动地点', trigger: 'blur' }
        ],
        startAt: [
          { required: true, message: '请输入开始时间', trigger: 'blur' }
        ],
        endAt: [
          { required: true, message: '请输入结束时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getExpert()
    this.getClient()
  },
  methods: {
    /** 查询活动列表 */
    getList() {
      this.loading = true
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    getClient() {
      listClient().then(response => {
        this.clientList = response.rows.map(item => {
          return {
            key: item.id,
            label: item.name
          }
        })
      })
    },
    getExpert() {
      listExpert().then(response => {
        this.expertList = response.rows.map(item => {
          return {
            key: item.id,
            label: item.name
          }
        })
      })
    },

    openExpert(row) {
      this.editExpertForm.activityId = row.id
      this.expertDialogVisible = true
      this.editExpertForm.expertIdList = row.expertList.map(item => item.id)
    },

    openClient(row) {
      this.editClientForm.clientIdList = row.clientList.map(item => item.id)
      this.editClientForm.activityId = row.id
      this.clientDialogVisible = true
    },

    /** 选择专家提交确认 */
    subExpertCheck(){
      if(this.editExpertForm.expertIdList.length === 0){
        this.$message.error('请选择专家')
        return
      }
      editExpert(this.editExpertForm).then(response => {
        this.expertDialogVisible = false
        this.getList()
      })
    },

    /** 选择专家提交确认 */
    subClientCheck(){
      if(this.editClientForm.clientIdList.length === 0){
        this.$message.error('请选择客户')
        return
      }
      editClient(this.editClientForm).then(response => {
        this.clientDialogVisible = false
        this.getList()
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        startAt: null,
        endAt: null,
        address: null,
        images: null,
        desc: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加活动'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getActivity(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改活动'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addActivity(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除活动编号为"' + ids + '"的数据项？').then(function() {
        return delActivity(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }

  }
}
</script>
<style lang="scss" scoped>
.drawer-content {
  padding: 0 30px 80px 20px;
}

.drawer-footer {
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
  text-align: right;
  padding: 16px;
  width: 100%;
  border-top: 1px solid #eee;
  background-color: #fff;
}

</style>
